package com.cheyun.netsalev3.view.orderquanyi

import android.util.Log
import android.view.View
import android.view.animation.AlphaAnimation
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.databinding.FragmentOrderQuanyiListBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.orderquanyi.OrderQuanyiViewModel
import com.google.android.material.tabs.TabLayout
import com.jeremyliao.liveeventbus.LiveEventBus

class OrderQuanyiFragment : BaseFragment<FragmentOrderQuanyiListBinding>() {
    override fun initContentView(): Int {
        return R.layout.fragment_order_quanyi_list
    }

    var titles = arrayListOf("全部", "未完成","已完成")
    var nkeys = arrayListOf("","0","1")
    private var listFragmet = ArrayList<Fragment>()

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            val factory = InjectorUtils.getOrderQuanyiListViewModelFactory(nkeys)
            return ViewModelProviders.of(this, factory).get(OrderQuanyiViewModel::class.java)
        }
        return null
    }

    override fun initView() {
        for (i in titles.indices) {
            listFragmet.add(OrderQuanyItemFragment.newInstance(nkeys[i]))
        }

        val pagerAdapter = HomePageAdapter(activity?.supportFragmentManager!!, listFragmet)
        pagerAdapter.titles = titles
        binding.secondViewPager.offscreenPageLimit = 4
        binding.secondViewPager.adapter = pagerAdapter
        binding.secondTabLayout.setupWithViewPager(binding.secondViewPager)
        //选中的位置
        binding.secondTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(p0: TabLayout.Tab?) {
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }

            override fun onTabSelected(p0: TabLayout.Tab?) {
                var pos = binding.secondTabLayout.selectedTabPosition
                Log.i("current pos =>",pos.toString())
                var nke = nkeys[pos]
                if(binding.viewModel?.totlelist!![nke] ==null){
                    binding.totleText.text = ""
                }else{
                    binding.totleText.text = titles[pos] + binding.viewModel?.totlelist!![nke]?.value + "人"
                }
                showTips()
            }
        })
        if(nkeys.size>0){
            for (index in 0 until nkeys.size){
                var nke = nkeys[index]
                binding.viewModel?.totlelist!![nke]?.observe(this@OrderQuanyiFragment, Observer {
                    if(index == binding.secondTabLayout.selectedTabPosition){
                        Log.i("current pos2 =>",index.toString())
                        binding.totleText.text = titles[index] + it.toString() + "人"
                        if(it!=null) {
                            showTips()
                        }
                    }
                })
            }
        }


        LiveEventBus.get()
                .with("order_quanyi_refresh", String::class.java)
                .observe(this, Observer {
                    setSearch()
                })
    }

    override fun initData() {

    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit OrderQuanyiFragment")
    }

    private fun showTips(){
        binding.totleText.setVisibility(View.VISIBLE)
        binding.totleText.clearAnimation()
        val animation = AlphaAnimation(1f, 0f)
        animation.duration = 3000
        binding.totleText.startAnimation(animation)
        binding.totleText.setVisibility(View.GONE)
    }

    fun setSearch(hashMap:HashMap<String, String>? = null){
        for (key in nkeys) {
            binding.viewModel?.refresh(key,hashMap)
        }
    }
}
